<?php
/**
 * @author Feng
 * @date 2012-5-28
 */
class controller_admin_collection_site extends system_user_control {
	
	private $concept = null;
	public function init() {
		if(!parent::init()) {
			return ;
		}
		picture_category::$_lang = $this->user->getLang();
		$this->concept = new collection_site_concept($this->user->getLang());
		$this->_crumbs()->addPage(system_nav_page::create($this->language('Collections'))->setRoute('collection/index'));
		$this->_crumbs()->addPage(system_nav_page::create($this->language('Sites'))->setRoute('collection/site/index'));
	}
	
	public function action_addnew() {
		$row = $this->concept->create();
		$this->view->row = $row;
		$row->setUrl('http://');
		$row = $this->_saveRow($row);
		$this->view->referer = $this->referer;
		$this->_crumbs()->addPage(system_nav_page::create($this->language('New Site'))->setRoute('collection/site/addnew'));
		$this->render('collection/site/row.phtml');
	}
	
	public function action_collect() {
		$id = intval($this->_param('id'));
		if(!($row = $this->concept->fetch(array('site.id = ?' => $id)))) {
			throw new collection_exception("The Site is not Existing.");
		}
		if($this->getRequest()->isPost()) {
			try {			
				if($this->_post('collect')) {
					$rule = $this->_post('rule');
					if(isset($rule['index'])) {
						$row->setRule($rule['index']);
					}
					$newRule = $this->_post('newRule');
					if(isset($newRule['index'])) {
						$row->setRuleId(0);
					}
					$row->save();
					$items = $row->getRule()->Collect();
					$this->setItemViewExisting($items);
					$this->view->items = $items;
				}
				if($this->_post('save')) {
					$this->view->items = $items = (array) $this->_post('item');
					$this->setItemViewExisting($items);
					$concept = new collection_item_concept();
					foreach($items AS $key => $item) {
						if(!($iRow = $concept->fetch(array('item.link = ?' => $key)))) {
							$iRow = $concept->create();
							$iRow->setSiteId($id);
							$iRow->setRuleId($row->getItemRuleId());
							$iRow->setType('item');
							$iRow->setRule((string) $row->getItemRule());
							$iRow->setTitle(trim($item['title']));
							$iRow->setUrl(trim($item['url']));
							$iRow->setKey($key);
						}
						$iRow->save();
					}
					$this->view->throwMessage = 'Complete';
				}				
			} catch (Exception $e) {
				$this->view->throwException = $e;
			}
		}

		$this->view->row = $row;
		$this->_crumbs()->addPage(system_nav_page::create($row->getTitle())->setRoute('collection/site/edit')->setParams(array('id' => $row->getId())));
		$this->_crumbs()->addPage(system_nav_page::create($this->language('Collect Item'))->setRoute('collection/site/collect')->setParams(array('id' => $row->getId())));
		$this->render();
	}
		
	private function setItemViewExisting($items) {
		$existing = array();
		$concept = new collection_item_concept();
		$keys = "'" . implode("', '", array_keys($items)) . "'";
		foreach($concept->fetchAll("item.`link` IN($keys)") AS $link) {
			$existing[$link->getLink()] = $link->getLink();
		}
		$this->view->existing = $existing;
	}
	
	public function action_edit() {
		$id = intval($this->_param('id'));
		if(!($row = $this->concept->fetch(array('site.id = ?' => $id)))) {
			throw new collection_exception("The Site is not Existing.");
		}
		$row = $this->_saveRow($row);
		$this->view->row = $row;
		$this->view->referer = $this->referer;
		$this->_crumbs()->addPage(system_nav_page::create($row->getTitle())->setRoute('collection/site/edit')->setParams(array('id' => $row->getId())));
		$this->render('collection/site/row.phtml');
	}
	
	private function _saveRow($row) {
		if(!$this->getRequest()->isPost()) {
			return $row;
		}
		$this->referer = $this->_post('referer');
		$rule = $this->_post('rule');
		$row->setName(trim($this->_post('name')));
		$row->setTitle(trim($this->_post('title')));
		$row->setCharset(trim($this->_post('charset')));
		$row->setUrl(trim($this->_post('url')));
		$row->setDescription($this->_post('description'));
		if(isset($rule['index'])) {
			$row->setRule($rule['index']);
		}
		$row->setIsActive(intval($this->_post('is_active')) ? 1 : 0);

		$rConcept = new collection_rule_concept();
		// save item rule
		if(isset($rule['item'])) {
			try {
				$rid = intval($row->getItemRuleId());
				if(!($rRow = $rConcept->fetch(array('id = ?' => $rid)))) {
					$rRow = $rConcept->create();
					$rRow->setType('item');
				}
				$rRow->setRule($rule['item']);
				$rRow = $rRow->save();
				$row->setItemRuleId($rRow->getId());
			} catch (Exception $e) {
				$this->view->throwException = $e;
			}
		}
		// save special rule
		if(isset($rule['special'])) {
			try {
				$sid = intval($row->getSpecialRuleId());
				if(!($sRow = $rConcept->fetch(array('id = ?' => $sid)))) {
					$sRow = $rConcept->create();
					$sRow->setType('special');
				}
				$sRow->setRule($rule['special']);
				$sRow = $sRow->save();
				$row->setSpecialRuleId($sRow->getId());
			} catch (Exception $e) {
				$this->view->throwException = $e;
			}
		}
		
		$this->_saveToDatabase($row, 'cover', 'image');		
		return $row;
	}
	
	public function action_index() {
		$this->_goRoute('collection/site/list');
		$this->render();
	}
	
	public function action_list() {
		$adapter = new system_paginator_adapter_concept($this->concept);
		$adapter->setCurrentPageNumber(intval($this->getRequest()->getParam('page', 1)));
		$adapter->setLimit(30);
		$adapter->order(array("site.id DESC"));
		$this->view->list = new system_paginator($adapter);;
		$this->_crumbs()->addPage(system_nav_page::create($this->language('List'))->setRoute('collection/site/list'));
		$this->render();
	}
}
